package com.samsung.service.impl.vote;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.samsung.common.util.BaseUtil;
import com.samsung.dao.vote.VoteHistoryMapper;
import com.samsung.dto.vote.VoteActionDto;
import com.samsung.dto.vote.VoteDetailDto;
import com.samsung.model.vote.VoteRecord;
import com.samsung.model.vote.VoteTopic;
import com.samsung.service.vote.TopicService;
import com.samsung.service.vote.VoteHistoryService;


/**
 * 
 *	@ClassName: VoteHistoryServiceImpl
 *	@Description: 投票记录表业务类
 *	@author pu.huang
 *	@date 2015年4月24日 下午5:26:02
 *
 */
@Service("voteHistoryService")
public class VoteHistoryServiceImpl implements VoteHistoryService {
	
	@Resource
	private VoteHistoryMapper voteHistoryMapper;

	@Autowired
	private TopicService topicService;
	
	@Override
	public int getCountByUserAndIP(String tid, String uid, String ip) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("tid", tid);
		map.put("uid", uid);
		map.put("ip", ip);
		return voteHistoryMapper.getCountByUserAndIP(map);
	}
	/**
	 *  保存投票记录方法
	 */
	@Override
	public int saveVoteHistory(VoteRecord voteHistory) {
		return voteHistoryMapper.insert(voteHistory);
	}
	@Override
	public Map<String, String> checkVoteDetail(VoteActionDto voteActionDto) {
		// 校验逻辑
		Map<String, String> resultMap = new HashMap<String, String>();
		// --------------------------------------------------------------
		
		int count = this.getCountByUserAndIP(voteActionDto.getTid(), voteActionDto.getUid(), voteActionDto.getIp());
		if( count>0 ){
			resultMap.put("flag", "failure");
			resultMap.put("message", "您已经投过票了！");
			return resultMap;
		}
		if (BaseUtil.isEmpty(voteActionDto.getTid())) {
			resultMap.put("flag", "failure");
			resultMap.put("message", "投票主题编号不能为空！");
			return resultMap;
		}

		VoteTopic topic = topicService.getTopic(voteActionDto.getTid());
		if (topic == null) {
			resultMap.put("flag", "failure");
			resultMap.put("message", "该投票主题不存在或已被删除！");
			return resultMap;
		}
		
		String type = this.getVoteType(voteActionDto.getTid());
		if( "0".equals(type) ){
			if (BaseUtil.isEmpty(voteActionDto.getOid())) {
				resultMap.put("flag", "failure");
				resultMap.put("message", "请至少选择一个投票项！");
				return resultMap;
			}
		}else{
			if( voteActionDto.getOids().length<=0 || voteActionDto.getOids() == null ){
				resultMap.put("flag", "failure");
				resultMap.put("message", "请至少选择一个投票项！");
				return resultMap;
			}
		}
		
		
		
		if (voteActionDto.getIp() == null) {
			resultMap.put("flag", "failure");
			resultMap.put("message", "您的IP无法获取！");
			return resultMap;
		}
		return resultMap;
	}
	@Override
	public Map<String, String> saveVoteAction(VoteActionDto voteActionDto) {
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap = this.checkVoteDetail(voteActionDto);
		if( resultMap.size()>0 ){
			return resultMap;
		}else{
			String type = this.getVoteType(voteActionDto.getTid());
			if ("0".equals(type)) {
				VoteRecord voteHistory = new VoteRecord();
//				voteRecord = voteActionDto.getVoteRecord();
				voteHistory.setHid(BaseUtil.getUUID());
				voteHistory.setIp(voteActionDto.getIp());
				voteHistory.setOid(voteActionDto.getOid());
				voteHistory.setTid(voteActionDto.getTid());
				voteHistory.setUid(voteActionDto.getUid());
				voteHistory.setVoteTime(new Date());
				int count = voteHistoryMapper.insert(voteHistory);
				if (count > 0) {
					resultMap.put("flag", "success");
					resultMap.put("message", "投票成功！");
				} else {
					resultMap.put("flag", "failure");
					resultMap.put("message", "投票失败！");
				}
			}
			/**
			 * 多选保存
			 */
			else {
				String[] oidArr = voteActionDto.getOids();
				for (int i = 0; i < oidArr.length; i++) {
					if( oidArr[i] != null && !"".equals(oidArr[i]) ){
						VoteRecord voteRecord = new VoteRecord();
						voteRecord.setHid(BaseUtil.getUUID());
						voteRecord.setIp(voteActionDto.getIp());
						voteRecord.setOid(oidArr[i]);
						voteRecord.setTid(voteActionDto.getTid());
						voteRecord.setUid(voteActionDto.getUid());
						voteRecord.setVoteTime(new Date());
						int count = voteHistoryMapper.insert(voteRecord);
						if( count<=0 ){
							resultMap.put("flag", "failure");
							resultMap.put("message", "投票失败！");
							return resultMap;
						}
					}
				}
				resultMap.put("flag", "success");
				resultMap.put("message", "投票成功！");
			}
		}
		return resultMap;
	}
	@Override
	public String getVoteType(String tid) {
		return voteHistoryMapper.getVoteType(tid);
	}
	@Override
	public List<Map<String, Object>> getVoteDetail(String tid) {
		return voteHistoryMapper.getVoteDetail(tid);
	}
	@Override
	public ModelAndView getView(String tid, String uid, String ip) {
		int count = this.getCountByUserAndIP(tid, uid, ip);
		if( count>0 ){
			return new ModelAndView("vote/voteResult");
		}else{
			return new ModelAndView("vote/voteDetail");
		}
	}
	@Override
	public List<VoteDetailDto> getVoteDetailCt(String tid) {
		return voteHistoryMapper.getVoteDetailCt(tid);
	}
	 
}
